package com.songlanyun.common.drive;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.songlanyun.common.enums.IncrementConstant;
import com.songlanyun.common.utils.RedisUtils;
import com.songlanyun.common.utils.SpringContextUtils;
import lombok.Data;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;

/**
 * 增加量
 *
 * @author WuChao
 */
@Data
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class Increment implements Serializable {

    /**
     * 浏览量
     */
    private Integer browseSum;

    /**
     * 分享量
     */
    private Integer shareSum;

    /**
     * @param operationType 0：查看 1：增加
     * @param scalarType    0：浏览量 1：分享量
     * @param redisKey      key
     */
    public void operationSum(IncrementConstant.OperationType operationType, IncrementConstant.ScalarType scalarType, String redisKey) {
        RedisUtils redisUtils = SpringContextUtils.getBean("redisUtils", RedisUtils.class);
        //增加浏览次数
        String sumRedis = redisUtils.get(redisKey);
        int sum = 0;
        switch (operationType) {
            case QUERY:
                if (StringUtils.isNotBlank(sumRedis)) {
                    sum = Integer.parseInt(sumRedis);
                }
                break;
            case ADD:
                sum = StringUtils.isNotBlank(sumRedis) ? Integer.parseInt(sumRedis) + 1 : 1;
                redisUtils.set(redisKey, sum);
                break;
            default:
        }

        switch (scalarType) {
            case BROWSE:
                this.setBrowseSum(this.getBrowseSum() + sum);
                break;
            case SHARE:
                this.setShareSum(this.getShareSum() + sum);
                break;
            default:
        }
    }

}
